import { http } from "@/utils/http";
import type { ArticleTagInfo } from "./article-tag";

/** 统一响应格式 */
export interface ApiResponse<T = any> {
  /** 状态码 */
  code: number;
  /** 响应消息 */
  message: string;
  /** 响应数据 */
  data?: T;
  /** 时间戳 */
  timestamp?: number;
}

// 首页数据类型定义
export interface Category {
  id: number;
  name: string;
  description: string;
  articleCount: number;
  icon: string;
  color: string;
}

export interface Article {
  id: number;
  title: string;
  summary: string;
  coverImage?: string;
  authorId: number;
  authorName: string;
  authorNickName: string;
  authorAvatar: string;
  categoryId: number;
  categoryName: string;
  tags: string[];
  /** 标签详细信息（新增） */
  articleTags?: ArticleTagInfo[];
  /** 标签数量（新增） */
  tagCount?: number;
  viewCount: number;
  likeCount: number;
  commentCount: number;
  collectCount: number;
  isTop: boolean;
  isOriginal: boolean;
  publishTime: string;
  readingTime: number;
  /** 当前用户是否已点赞（后端返回） */
  isLiked?: boolean;
  /** 当前用户是否已收藏（后端返回） */
  isCollected?: boolean;
}

export interface ArticleList {
  records: Article[];
  total: number;
  current: number;
  size: number;
  pages: number;
  hasNext: boolean;
}

export interface RecommendedAuthor {
  userId: number;
  userName: string;
  nickName: string;
  avatar: string;
  articleCount: number;
  totalViews: number;
  totalLikes: number;
  followerCount: number;
  /** 当前用户是否已关注该作者 */
  isFollowed?: boolean;
}

export interface HomeData {
  categories: Category[];
  articles: ArticleList;
}

// 首页数据查询参数
export interface HomeQueryParams {
  current?: number;
  size?: number;
  categoryId?: number;
  tagName?: string;
  keyword?: string;
  sortType?: "latest" | "hot" | "liked" | "commented";
  onlyTop?: boolean;
  onlyOriginal?: boolean;
}

/**
 * 获取分类列表
 */
export const getCategories = () => {
  return http.request<ApiResponse<Category[]>>("get", "/api/home/categories");
};

/**
 * 获取文章列表
 */
export const getArticles = (params?: HomeQueryParams) => {
  return http.request<ArticleList>("get", "/api/home/articles", { params });
};

/**
 * 获取推荐作者
 */
export const getRecommendedAuthors = (params?: { limit?: number }) => {
  return http.request<RecommendedAuthor[]>(
    "get",
    "/api/home/authors/recommended",
    {
      params
    }
  );
};
